<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('jquery-ui/ui/jquery.ui.sortable.js'))
->appendFile($this->js('jquery-ui/ui/jquery.ui.draggable.js'))
->appendFile($this->js('jquery-ui/ui/jquery.ui.resizable.js'))
->appendFile($this->js('plugins/jquery.alerts.js'))
->appendFile($this->js('plugins/jquery.bt.min.js'));
$this->headLink()->appendStylesheet($this->js('css/jquery.alerts.css'))
->appendStylesheet($this->js('css/jquery.bt.css'));
?>
<script>
function getStudent() {
    var year_semester = $("#year_semester").val();
    var classSn = $("#classSn").val();
    $.get('<?php echo $this->route("get-student")?>',
            {year_semester: year_semester, classSn:classSn},
            function(data){
               $("#student-list").html(data);
            });
}

$(document).ready(function(){

	$("#year_semester").change(function(){
	    $("#selectForm").submit();
	});

	$("#classSn").change(getStudent);
    // 自動填入座號
	$("#autoSignBtn").click(function(){
        $.each($("#sortable .numbers"), function(i,v){
            $(v).attr('value',i+1);
         });
        $('#sortForm').submit();
     });

    // 儲存座號
    $("#saveBtn").click(function(){
        $('#setForm').submit();
        getStudent();
     });

    $(".new_student_id").live('change',function(){
        var curr = $(this);
        $.getJSON('<?php echo $this->route("get-student-by-id")?>',
          { id: curr.attr('value'),
            year_semester : $("#year_semester").val(),
            classSn : $("#classSn").val()
          },
          function(response){
        	    if (response.success) {
            	    var number = $(curr).parent('td').parent('tr').children('td').children('input.numbers');
            	    var student_name = $(curr).parent('td').parent('tr').children('td.student_name');
            	    $(curr).attr('name','new_student_id['+response.data.sn+']');
            	    $(number).attr('name','sn['+response.data.sn+']');
        	    	$(student_name).html(response.data.name);
        	    }
        	    else
            	    alert('<?php echo $this->translate("no student")?>');
          });
     });
    getStudent();
});
</script>
<div id="select-area">
<form action="" method="post" id="selectForm">
<select id="year_semester" name="year_semester">
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select name="classSn" id="classSn">
<?php echo $this->classNameOptions($this->yearSemester,$this->classSn)?>
</select>

<input type="button"  id="autoSignBtn" class="fg-button ui-state-default ui-corner-all "
value="<?php echo $this->translate('auto sign number')?>" style="margin:1px;font-size:11px" />
<input type="button"  id="saveBtn" class="fg-button ui-corner-all ui-state-default "
value="<?php echo $this->translate('save number')?>" style="margin:1px;font-size:11px" />
</form>
</div>
<div id="student-list"></div>
<input type="button"  id="appendBtn" class="fg-button ui-corner-all ui-state-default "
value="<?php echo $this->translate('append number')?>" style="margin:1px;font-size:11px" />
